package com.construction_Safety.region.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.construction_Safety.common.annotation.Log;
import com.construction_Safety.common.core.controller.BaseController;
import com.construction_Safety.common.core.domain.AjaxResult;
import com.construction_Safety.common.enums.BusinessType;
import com.construction_Safety.region.domain.CUserRegion;
import com.construction_Safety.region.service.ICUserRegionService;
import com.construction_Safety.common.utils.poi.ExcelUtil;
import com.construction_Safety.common.core.page.TableDataInfo;

/**
 * 员工区域管理Controller
 * 
 * @author yat
 * @date 2025-06-10
 */
@RestController
@RequestMapping("/region/region_user")
public class CUserRegionController extends BaseController
{
    @Autowired
    private ICUserRegionService cUserRegionService;

    /**
     * 查询员工区域管理列表
     */
    @PreAuthorize("@ss.hasPermi('region:region_user:list')")
    @GetMapping("/list")
    public TableDataInfo list(CUserRegion cUserRegion)
    {
        startPage();
        List<CUserRegion> list = cUserRegionService.selectCUserRegionList(cUserRegion);
        return getDataTable(list);
    }

    /**
     * 导出员工区域管理列表
     */
    @PreAuthorize("@ss.hasPermi('region:region_user:export')")
    @Log(title = "员工区域管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CUserRegion cUserRegion)
    {
        List<CUserRegion> list = cUserRegionService.selectCUserRegionList(cUserRegion);
        ExcelUtil<CUserRegion> util = new ExcelUtil<CUserRegion>(CUserRegion.class);
        util.exportExcel(response, list, "员工区域管理数据");
    }

    /**
     * 获取员工区域管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('region:region_user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(cUserRegionService.selectCUserRegionById(id));
    }

    /**
     * 新增员工区域管理
     */
    @PreAuthorize("@ss.hasPermi('region:region_user:add')")
    @Log(title = "员工区域管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CUserRegion cUserRegion)
    {
        List<Integer> cUserRegions = cUserRegionService.selectCUserRegionByUserId(cUserRegion.getUserId());
        if(cUserRegions.contains(cUserRegion.getRegionId())){
            return error("该员工已在该区域工作，不能重复添加");
        }
        return toAjax(cUserRegionService.insertCUserRegion(cUserRegion));
    }

    /**
     * 修改员工区域管理
     */
    @PreAuthorize("@ss.hasPermi('region:region_user:edit')")
    @Log(title = "员工区域管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CUserRegion cUserRegion)
    {
        List<Integer> cUserRegions = cUserRegionService.selectCUserRegionByUserId(cUserRegion.getUserId());
        if(cUserRegions.contains(cUserRegion.getRegionId())){
            return error("该员工已在该区域工作，不能重复添加");
        }
        return toAjax(cUserRegionService.updateCUserRegion(cUserRegion));
    }

    /**
     * 删除员工区域管理
     */
    @PreAuthorize("@ss.hasPermi('region:region_user:remove')")
    @Log(title = "员工区域管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(cUserRegionService.deleteCUserRegionByIds(ids));
    }
}
